//
//  CZHomeViewController.swift
//  weiBo
//
//  Created by apple on 16/5/10.
//  Copyright © 2016年 itcast. All rights reserved.
//

import UIKit

class CZHomeViewController: CZVisitorTableController {
    //获取微博数据
//    var statusList : [CZStatus]?
    private var statusListViewModel: CZStatusListViewModel = CZStatusListViewModel()
    //懒加载一个上拉刷新的空间
    private lazy var pullUpView: UIActivityIndicatorView = {
        let view = UIActivityIndicatorView(activityIndicatorStyle: UIActivityIndicatorViewStyle.WhiteLarge)
        view.color = UIColor.redColor()
        
        return view
    }()
    //懒加载下拉刷新控件
//    private lazy var refreshCtr: UIRefreshControl = {
//        let refresh = UIRefreshControl()
//        refresh.addTarget(self, action: "pullDownRefresh", forControlEvents: .ValueChanged)
//        return refresh
//    }()
    //懒加载第三放
    private lazy var refreshCtr: CZRefreshControl = {
        let refresh = CZRefreshControl()
        refresh.addTarget(self, action: "pullDownRefresh", forControlEvents: .ValueChanged)
        
        return refresh
    }()
    override func viewDidLoad() {
        super.viewDidLoad()
        
        if userLogin {
            loadStatus()
        
            setUpTableView()
        } else {
            
            visitorView?.setVistorInfor(nil, imageName: nil)
        }
    }
    
    private func setUpTableView() {
        tableView.registerClass(CZStatusTableViewCell.self, forCellReuseIdentifier: "cell")
        tableView.rowHeight = UITableViewAutomaticDimension
        tableView.estimatedRowHeight = 200
        //添加上拉刷新视图
        tableView.tableFooterView = pullUpView
        pullUpView.sizeToFit()

        //添加下拉刷新
        //第一种方式
//        self.refreshControl = refreshCtr
        //第二种方式
        tableView.addSubview(refreshCtr)
        
    }
    //下拉刷新2s结束
    @objc private func pullDownRefresh() {
        //加载数据
        loadStatus()
    }
}


//tableView的数据源代理方法
extension CZHomeViewController {

    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return statusListViewModel.statusList?.count ?? 0
    }
    
    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! CZStatusTableViewCell
        cell.statusViewModel = statusListViewModel.statusList![indexPath.row]
        cell.selectionStyle = .None
        
        return cell
    }
    
    override func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {
        if indexPath.row == statusListViewModel.statusList!.count - 1 && !pullUpView.isAnimating() {
//            print("这是最后1个了")
            pullUpView.startAnimating()
            
            loadStatus()
        }
    }
    
    private func loadStatus() {
        statusListViewModel.requestStatus(pullUpView.isAnimating()) { (isSuccess) -> () in
            self.endRefreshing()
            if isSuccess {
                self.tableView.reloadData()
            }
            
        }
    }
    
    private func endRefreshing() {
        pullUpView.stopAnimating()
        refreshCtr.endRefreshing()
    }
    
    
    
    
}

